package p2plending.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import p2plending.domain.Usuario;
import p2plending.domain.Valoracion;
import p2plending.utils.UIDGenerator;

public class JDBCValoracionDAO implements IValoracionDAO{
	
	private Connection conn;
	private IUsuarioDAO iu;
	
	public JDBCValoracionDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Se ha creado la conexion correctamente");
		iu= new JDBCUsuarioDAO();
	}

	public void delete(String valoracionOID) {
        String sql = "DELETE FROM valoracion WHERE (valoracionOID = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, valoracionOID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
	}
	
	public void insert(Valoracion v, Usuario u) {
		PreparedStatement stmt = null;
        String sql = "INSERT INTO valoracion (valoracionOID, valoracionID, usuarioOID, puntuacion, comentario) VALUES (?, ?, ?, ?, ?)";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, UIDGenerator.getInstance().getKey());
            stmt.setString(2, v.getValoracionID());
            stmt.setString(3, iu.selectOIDUsuario(u.getNombreUsuario()));
            stmt.setFloat(4, v.getPuntuacion());
            stmt.setString(5, v.getComentario());

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }	
	}

	public Valoracion select(String valoracionOID) {
		String sql = "SELECT * FROM valoracion WHERE (valoracionOID = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Valoracion v= null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, valoracionOID);
            result = stmt.executeQuery();

            result.next();
            v = new Valoracion();
            v.setValoracionID(result.getString("valoracionID"));
            v.setPuntuacion(result.getFloat("puntuacion"));
            v.setComentario(result.getString("comentario"));

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return v;
    }
}